package com.hualan.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hualan.mapper.WelfareMapper;
import com.hualan.pojo.PageResult;
import com.hualan.pojo.Result;
import com.hualan.pojo.Welfare;
import com.hualan.service.WelfareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WelfareServiceImpl implements WelfareService {
    @Autowired
    private WelfareMapper welfareMapper;
    @Override
    public PageResult<Welfare> queryAllWelfare(String title, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Welfare> welfareList = welfareMapper.queryAllWelfare(title);
        Page<Welfare> p = (Page<Welfare>) welfareList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public Result addWelfare(Welfare welfare) {
        try {
            welfareMapper.addWelfare(welfare);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        }
    }

    @Override
    public Result updateWelfare(Welfare welfare) {
        try {
            welfareMapper.updateWelfare(welfare);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("修改失败");
        }
    }

    @Override
    public Result deleteWelfare(List<Integer> ids) {
        try {
            welfareMapper.deleteWelfare(ids);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }
}
